/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-1-14 下午04:34:38
 *
 * ChangeEmailAction.java
 *
 *******************************************************************/
package com.transer.web.action.passport;

import org.apache.log4j.Logger;

import com.transer.persistance.bean.User;
import com.transer.persistance.dao.IUserDao;
import com.transer.web.action.JsonActionSupport;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class ChangeEmailAction extends JsonActionSupport {

	private static final Logger logger = Logger.getLogger(ChangeEmailAction.class);

	private static final long serialVersionUID = 3518599158920775725L;
	// 用户操作
	private IUserDao userDao;
	// 电子邮件
	private String email;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// 原始邮箱
		String original = user.getEmail();
		// 更新是否成功
		boolean success = false;

		try {
			// 查看EMAIL是否存在
			User existUser = userDao.isEmailExsit(email);
			// 存在
			if (existUser != null) {
				// 返回存在消息
				writeSimpleJsonMessage(false, getText("error.email.exsit"));
				return null;
			}
			// 设置新邮箱
			user.setEmail(email);
			// 更新用户
			userDao.updateUser(user);
			// 更新成功
			success = true;
		} catch (Exception e) {
			e.printStackTrace();
			// 将邮箱更改回原来邮箱
			user.setEmail(original);
			// 记录日志
			logger.error("修改用户邮箱出错", e);
		}
		// 返回消息
		writeSimpleJsonMessage(success, success ? "" : getText("error.email.change"));

		return null;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the userDao
	 */
	public IUserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
}
